home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 234_01 / getapage.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-06-16  |  11.9 KB  |  448 lines

  1. /*
  2.   HEADER: CUG     nnn.nn;
  3.   TITLE:     XDIR - Hard Disk Manager
  4.   VERSION:     1.0 for IBM-PC
  5.   DATE:      Apr 03, 1987
  6.   DESCRIPTION:     Hard Disk Manager for IBM PC
  7.   KEYWORDS:     Hard Disk Manager Dump Directory
  8.   SYSTEM:     IBM-PC and Compatiables
  9.   FILENAME:      getapage.c
  10.   WARNINGS:     None
  11.   CRC:         N/A
  12.   SEE-ALSO:     HDIR.DOC and XDIR.DOC
  13.   AUTHOR:     Mike Blakley 15645 SW 82 Cir Ln #76, Miami, Fl 33193
  14.   COMPILERS:     ECO-C
  15.   REFERENCES:     XDIR.DOC
  16. */
  17.  
  18.  
  19. /*
  20.    getapage
  21.    get a page at a time from a disc file
  22.    used in the routine XDIR to display files
  23. */
  24. #include "stdio.h"
  25. /* #define DEBUG 1  */
  26. #define MAXPAGE 1000
  27. #define PAGESIZE 2000
  28. /*
  29.    typ
  30.    type a file to the console
  31.  
  32. */
  33. void typ(fnam)
  34. char *fnam;
  35. {
  36.      static int  i,j,k,l,len;
  37.      static int  found,m,c;
  38.      static int  nflag;
  39.      static int  fd;
  40.      static int  sflag, oldi;
  41.      static int  currpage, lastpage;
  42.      static int  row, col;
  43.      static char temp[80];
  44.      static char temp1[80];
  45.      static char sword[80];       /* search word */
  46.      static char xtemp[PAGESIZE]; /* page buffer */
  47.      static char xsave[PAGESIZE]; /* page buffer */
  48.      struct {
  49.         long offs;        /* offset in file */
  50.         int  plen;        /* length of page */
  51.         } pages[MAXPAGE];
  52.      long foff;
  53.  
  54.      k = l = len = 0;
  55.      i = j = 1;
  56.      foff = 0L;
  57.  
  58.      while (1)     /* build array of file info */
  59.      {
  60.      j = getxline(temp,80,fnam,i);
  61.      if (j == -1) break;
  62.      i = 0;
  63.      len += j;    /* update length */
  64.      ++l;         /* line counter */
  65.      if ((l%23) == 0)
  66.         {
  67.         pages[k].offs = foff;
  68.         pages[k].plen = len;
  69.         foff += (long) len;
  70.         len = 0;
  71.         ++k;
  72.         if (k >= MAXPAGE) {--k;break;}
  73.         }
  74.      }
  75.  
  76.      if (len > 0)
  77.        {
  78.        pages[k].offs = foff;    /* update last page */
  79.        pages[k].plen = len;
  80.        ++k;
  81.        }
  82.      lastpage = k;              /* note the last page */
  83.  
  84. #ifdef DEBUG
  85.      for (foff=0L,i=0;i<k;i++)      /* list the file info */
  86.      {
  87.      foff += (long) pages[i].plen;
  88.      }
  89.      printf("\nSize of %s is %ld",fnam,foff);
  90.      writestr("\nPress any key ... ");
  91.      getch();
  92.  
  93.      for (i=c=0;i<k;i++)              /* list the detail */
  94.      {
  95.      printf("\nPage %d offset %ld length %d",(i+1),pages[i].offs,
  96.            pages[i].plen);
  97.      if ((i%20) == 19)
  98.         {
  99.         writestr("\nNext page or X=cancel ");
  100.         c=getch();
  101.         }
  102.      if (c == 'X') break;
  103.      }
  104. #endif
  105.  
  106.      fd = open(fnam,0);
  107.      if (fd == -1) {writestr("\nCan't open ");
  108.                     writestr(fnam);
  109.                     exit(0);}
  110.  
  111.  
  112.      currpage = sflag = 0;              /* begin with page 0 */
  113.      while (1)
  114.      {
  115.      if (sflag == 0)
  116.      {
  117.      top(fnam,currpage);
  118.      foff = lseek(fd,pages[currpage].offs,0);
  119.      j = pages[currpage].plen;
  120.      if (j >= PAGESIZE) j = PAGESIZE;     /* prevent overflow */
  121.      read(fd,xtemp,j);
  122.      j -= 2;
  123.      xtemp[j] = 0;
  124.      writestr(xtemp);
  125.      bot();          /* write the bottom */
  126.      }
  127.      sflag = 0;
  128.      c =toupper(getch());
  129.      if (c == 0) c=getch();
  130.  
  131.      nflag = 0;        /* no numeric input */
  132.      switch (c)
  133.      {
  134.      case '\033':      /* escape */
  135.         break;
  136.      case 0x0d:
  137.      case 0x0a:
  138.         ++currpage;
  139.         break;
  140.      case 73:         /* page up */
  141.         --currpage;
  142.         break;
  143.      case 81:         /* page down */
  144.         ++currpage;
  145.         break;
  146.      case 71:         /* home */
  147.         currpage = 0;
  148.         break;
  149.      case 79:         /* end */
  150.         currpage = lastpage-1;
  151.         break;
  152.      case '+':
  153.      case '-':
  154.      case '0':
  155.      case '1':
  156.      case '2':
  157.      case '3':
  158.      case '4':
  159.      case '5':
  160.      case '6':
  161.      case '7':
  162.      case '8':
  163.      case '9':
  164.        putchar(c);    /* display it */
  165.        nflag = 1;
  166.        break;
  167.      case 59:         /* F1 = help */
  168.         clrscr();
  169.         writestr("Options are as follows: \n");
  170.         writestr("\nOption       Description");
  171.         writestr("\nPgUp         Displays the previous page");
  172.         writestr("\nPgDn         Displays the following page ");
  173.         writestr("\nHome         Displays the first page ");
  174.         writestr("\nEnd          Displays the last page ");
  175.         writestr("\n+nnn         Page forward nnn pages from here ");
  176.         writestr("\n-nnn         Page backward nnn pages from here ");
  177.         writestr("\nnnn          Go to absolute page nnn ");
  178.         writestr("\nEnter        Next page ");
  179.         writestr("\nF1           Help  - This page ");
  180.         writestr("\nS<string>    Search for text <string>");
  181.         writestr("\nInvalid key  Causes beep sound");
  182.         writestr("\n\n\nPress any key to continue");
  183.         getch();
  184.         break;
  185.      case 60:    /* F2 - display page layouts */
  186.         clrscr();
  187.         for (m=0,row=col=1;m<lastpage;m++)
  188.         {
  189.         cursor(row,col);
  190.         writestr("Pg ");
  191.         itoa(temp1,(m+1));
  192.         writestr(temp1);
  193.         writestr(" offs ");
  194.         ltoa(temp1,pages[m].offs);
  195.         writestr(temp1);
  196.         writestr(" len ");
  197.         itoa(temp1,pages[m].plen);
  198.         writestr(temp1);
  199.         ++row;
  200.         if (row > 23)
  201.            {
  202.            row = 1;
  203.            col +=25;
  204.            }
  205.         }
  206.         getch();
  207.         break;
  208.      case 'S':   /* search for text string */
  209.            sflag = 1;         /* set search flag */
  210.            break;
  211.      default:
  212.         putchar('\007');   /* beep */
  213.         break;
  214.      }          /* end case */
  215.  
  216.      if (c == '\033') break;     /* end of program */
  217.  
  218.      if (nflag)   /* numeric input */
  219.         {
  220.         gets(temp);      /* get numeric */
  221.         j = atoi(temp);
  222.         if (c == '+') currpage += j;
  223.         else if (c == '-') currpage -= j;
  224.         else {
  225.              temp1[0] = c;
  226.              temp1[1] = 0;
  227.              strcat(temp1,temp);
  228.              j = atoi(temp1);
  229.              currpage = (j-1);
  230.              }
  231.         }                /* end if numeric */
  232.  
  233.       if (c == 'S')      /* search string */
  234.         {
  235.         int  c1;
  236.         char *cpo;
  237.         cpo = temp;
  238.         oldi = currpage;          /* save page number */
  239.         putchar(c);
  240.         writestr("earch? ");
  241.  
  242.         while (1)
  243.         {
  244.         c1 = getch();
  245.         if (c1 == '\033') break;
  246.         if (c1 == '\015') break;
  247.         putchar(c1);
  248.         *cpo++ = c1;
  249.         }
  250.         *cpo = 0;
  251.  
  252.         l = strlen(temp);
  253.         if (c1 == '\033') break;      /* escape = no search */
  254.         if (l != 0)                   /* default previous text */
  255.             strcpy(sword,temp);
  256.  
  257.         for (;currpage<lastpage;currpage++)  /* search from current page forward */
  258.         {
  259.         int  n;
  260.         foff = lseek(fd,pages[currpage].offs,0);
  261.         j = pages[currpage].plen;
  262.         read(fd,xtemp,j);
  263.         j -= 2;
  264.         xtemp[j] = 0;
  265.  
  266.            for (m=found=0;m<j;m++)         /* search the buffer */
  267.            {
  268.            if ((n=strncmp(sword,xtemp+m,l)) == 0)
  269.               {found=1;break;}
  270.            }
  271.  
  272.            if (found)  break;             /* stop at first occurence */
  273.          }    /* end for (;i<k) */
  274.  
  275.         if (found)
  276.               {
  277.               top(fnam,currpage);
  278.               strncpy(xsave,xtemp,m);
  279.               xsave[m] = 0;
  280.               writestr(xsave);
  281.               writestr("\033[7m");
  282.               writestr(sword);
  283.               writestr("\033[0m");
  284.               strcpy(xsave,xtemp+m+strlen(sword));
  285.               writestr(xsave);
  286.               bot();
  287.               }
  288.         else
  289.              {
  290.              writestr(" Not found ");
  291.              currpage = oldi;
  292.              }
  293.         }      /* end if c == 'S' */
  294.  
  295.      if (currpage >= lastpage) currpage = lastpage-1;   /* max */
  296.      if (currpage < 1) currpage = 0;
  297.  
  298.      }       /* end while 1 */
  299.  
  300.      close(fd);
  301.  
  302.  
  303. }            /* end function typ */
  304.  
  305. getxline(buffer,maxlen,fnam,action)
  306. char *buffer;       /* output buffer */
  307. int  maxlen;        /* maximum length */
  308. char *fnam;         /* file name */
  309. int  action;        /* 0 = read, 1 =open, 2 = clse */
  310. {
  311.        static int fd;
  312.        static int eofind, index, priorbyte, ocount;
  313.        static int state, temp;
  314.        static char inbuff[258];
  315.        static int  eobbyte, lastblk;
  316.        static char c, *cpo;
  317. #ifdef DEBUG
  318.        char xtemp[20];
  319. #endif
  320.  
  321.        if (action == 2) {close(fd); return(0);}
  322.        if (action == 1) state = 0;
  323.        cpo = buffer;
  324.  
  325.  
  326.        while (1)
  327.        {
  328.            switch (state)
  329.            {
  330.            case 0:    /* initial state for open file */
  331.              cpo = buffer;
  332.              fd = open(fnam,0);
  333.              c = eofind = index = priorbyte = ocount = 0;
  334.              lastblk = 0;
  335.              if (fd == -1)
  336.                 {writestr("\nCan't open ");
  337.                 writestr(fnam);
  338.                 eofind = 1; ocount = -1; state = 90;}
  339.              else state = 1;
  340.              break;
  341.            case 1:    /* prepare to read a block */
  342.              if (lastblk == 1) {eofind = 1;state = 90;}
  343.                 else state = 2;
  344.              break;
  345.            case 2:   /* read a block */
  346.              index = 0;
  347.              eobbyte = read(fd,inbuff,256);
  348. #ifdef DEBUG
  349.              writestr("\nRead bytes ");
  350.              itoa(xtemp,eobbyte);
  351.              writestr(xtemp);
  352. #endif
  353.              if (eobbyte <= 0) {lastblk = 1; close(fd); state = 90;}
  354.              else
  355.              if (eobbyte < 256) {lastblk = 1; close(fd); state = 3;}
  356.              else state = 3;
  357.              break;
  358.  
  359.            case 3:   /* not used */
  360.            case 4:   /* determine if index > eobbyte */
  361.              if (index >= eobbyte) state = 1;
  362.                 else state = 5;
  363.              break;
  364.  
  365.            case 5:   /* transfer an output byte */
  366.               priorbyte = (int) c;    /* save prior byte */
  367.               c = *(inbuff + index);  /* get current byte */
  368.               ++index;                /* update the index */
  369.               ++ocount;               /* update the byte counter */
  370.               if (c == 0x0d) {*cpo++ = 0;state = 4;}
  371.               else
  372.               if (c == 0x0a) {state = 90;}     /* exit on LF */
  373.               else   {*cpo++ = c;state=20;}
  374.               break;
  375.  
  376.            case 10:      /* last byte of prior block was a CR */
  377.               c = *(inbuff+index);
  378.               if (c == 0x0a)
  379.                  {
  380.                  priorbyte = (int) c;
  381.                  ++index;
  382.                  ++ocount;
  383.                  }
  384.               state = 90;
  385.               break;
  386.  
  387.            case 20:      /* test length of string */
  388.               if (ocount >= maxlen)
  389.                  state = 90;
  390.               else state = 4;
  391.               break;
  392.  
  393.            case 80:       /* normal entry point */
  394.               if (eofind == 1) {ocount = -1;state = 90;}
  395.                 else state = 4;
  396.               break;
  397.  
  398.            case 90:       /* normal exit point */
  399.               *cpo++ = 0;
  400.               temp = ocount;
  401.               ocount = 0;
  402.               state = 80;
  403.               return temp;
  404.               break;
  405.  
  406.  
  407.            }        /* end switch */
  408.         }   /* end while */
  409.  
  410. }           /* end function */
  411.  
  412. /*
  413.    display top of page
  414.  
  415. */
  416. void top(fnam,i)
  417. char *fnam;      /* file name */
  418. int i;           /* page */
  419. {
  420.      char temp[10];
  421.  
  422.      clrscr();
  423.      writestr("Listing of file .. ");
  424.      writestr("\033[7m");
  425.      writestr(fnam);
  426.      writestr("\033[0m");
  427.      writestr("                 page ");
  428.      itoa(temp,(i+1));
  429.      writestr(temp);
  430.      putchar('\n');
  431.  
  432. }
  433.  
  434. /*
  435.   bot
  436.  
  437. */
  438. void bot()
  439. {
  440.      cursor(24,0);
  441.      writestr("\033[7m");
  442.      writestr("\nOptions:");
  443.      writestr("\033[0m");
  444.      writestr(" ESC = exit, PgUp, PgDn, Home, End, F1 = Help ... ");
  445.      writestr("\033[0m");
  446. }
  447.  
  448.